package com.rockbb.common.simpleframe.result;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.ObjectMapper;

import com.rockbb.common.utilities.text.JacksonMapper;
import com.rockbb.common.utilities.text.JacksonViews;

public class JSONProcessor {

	private static Logger logger = Logger.getLogger(JSONProcessor.class);

	public static void process(HttpServletResponse res, Object json_obj) 
	{
		process(res, json_obj, null);
	}

	public static void process(HttpServletResponse res, Object json_obj, String callback) 
	{
		ObjectMapper mapper = JacksonMapper.getInstance();
		try 
		{
			if (callback != null && callback.length() > 0) res.getWriter().write(callback+"(");
			mapper.viewWriter(JacksonViews.Public.class).writeValue(res.getWriter(), json_obj);
			if (callback != null && callback.length() > 0) res.getWriter().write(")");
		} 
		catch (JsonGenerationException e) 
		{
			logger.info("JsonGenerationException");
			logger.debug("Exception details:", e);
		} 
		catch (IOException e) 
		{
			logger.info("IOException");
			logger.debug("Exception details:", e);
		}
		catch (Exception e)
		{
			logger.info("Unknown Exception");
			logger.debug("Exception details:", e);
		}
	}
}